using System.Collections.Generic;
using Entitas;
using UnityEngine;

public sealed class ProcessShootInputSystem : ReactiveSystem {

    public ProcessShootInputSystem(Contexts contexts) : base(contexts.input) {
        _contexts = contexts;

        // TODO Put on a component
        _bulletsObjectPool = new ObjectPool<GameObject>(() => Assets.Instantiate<GameObject>(Res.Bullet));
    }

    protected override Collector GetTrigger(Context context) {
        return context.CreateCollector(InputMatcher.ShootInput);
    }

    protected override bool Filter(Entity entity) {
        // TODO Entitas 0.36.0 Migration
        // ensure was: 
        // exclude was: 

        return true;
    }

    readonly Contexts _contexts;
    ObjectPool<GameObject> _bulletsObjectPool;

    

    protected override void Execute(List<Entity> entities) {
        var input = entities[entities.Count - 1];
        var ownerId = input.inputOwner.playerId;

        var e = _contexts.core.GetEntityWithPlayerId(ownerId);
        if(!e.hasBulletCoolDown) {

            // TODO CoolDown should be configurable
            e.AddBulletCoolDown(7);

            var velX = GameRandom.core.RandomFloat(-0.02f, 0.02f);
            var velY = GameRandom.core.RandomFloat(0.3f, 0.5f);
            var velocity = new Vector3(velX, velY, 0);
            _contexts.blueprints.blueprints.instance.ApplyBullet(
                _contexts.bullets.CreateEntity(), e.position.value, velocity, _bulletsObjectPool
            );
        }
    }
}
